package sw6.digipecs.widget;

import sw6.digipecs.R;
import sw6.digipecs.model.PECSBook;
import sw6.digipecs.model.PECSBook.PECSBookChangeListener;
import sw6.digipecs.widget.dragdrop.DropTarget;
import android.content.Context;
import android.graphics.Rect;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

public class PageBarLayout extends LinearLayout implements PECSBookChangeListener, DropTarget {
	
	private LayoutInflater mInflater;
	private View mLastTouched;
	private ViewGroup mBar;
	private PECSBook mBook;
	private final Rect mRect = new Rect();

	public PageBarLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    if(mInflater != null){       
	    	mInflater.inflate(R.layout.pagebarlayout, this);
	    }
	    
	    mBar = (ViewGroup)findViewById(R.id.linear1);
	}
	
	public void setBook(PECSBook book) {
		mBook = book;
		
		// We would like to be informed very time book is changed, if we have to change the number of buttons.
		mBook.addOnChangeListener(this);
		updateButtons();
	}
	
	private void updateButtons() {
		TextView textView;
		
		mBar.removeAllViews();
		
		for (int pos = 0; pos < mBook.getPageCount(); pos++) {
			
			// Create text view and add it to bar
			textView = (TextView) mInflater.inflate(R.layout.pagebutton, mBar, false);
			mBar.addView(textView);
			
			// Set text
			textView.setText("" + (pos + 1));
			
			// Add first as mLastTouched, and set its background
			if(pos == mBook.getPage()){
				textView.setBackgroundResource(R.drawable.pagebuttonclicked);
				mLastTouched = textView;
			}
			
			// Add onClickListener
			final int id = pos;
			textView.setOnClickListener(new OnClickListener(){     	
				public void onClick(View v) {
											
					mLastTouched.setBackgroundResource(R.drawable.pagebutton);
					mLastTouched.invalidate();
								
					v.setBackgroundResource(R.drawable.pagebuttonclicked);
					v.invalidate();
					mLastTouched = v;
					
					mBook.setPage(id);
				}
			});
			
		}
	}
	
	private static class SavedState extends BaseSavedState {
		private int mPageNum;

		SavedState(Parcelable superState) {
			super(superState);
		}
		
		void putPageNum(int i) {
			mPageNum = i;
		}
		
		int getPageNum() {
			return mPageNum;
		}
	}

	@Override
	protected void onRestoreInstanceState(Parcelable state) {
		if (state instanceof SavedState) {
			SavedState ss = (SavedState) state;
			super.onRestoreInstanceState(ss.getSuperState());
			mBook.setPage(ss.getPageNum());
		} else {
			// This created an exception in rare cases...
			//super.onRestoreInstanceState(state);
			mBook.setPage(0);
		}
	}

	@Override
	protected Parcelable onSaveInstanceState() {
		Parcelable superState = super.onSaveInstanceState();
		SavedState ss = new SavedState(superState);
		
		ss.putPageNum(mBook.getPage());
		
		return ss;
	}

	@Override
	public void bookChanged() {
		updateButtons();
	}
	
	// TODO This is pretty close to copy of the method in SentenceBoardLayout
	private View findView(int x, int y) {
		final ViewGroup parent = mBar;
		final int count = parent.getChildCount();
		final Rect rect = mRect;
		
		for (int i = 0; i < count; i++) {
			final View v = parent.getChildAt(i);
			
			v.getHitRect(rect);
			if (rect.contains(x, y)) {
				return v;
			}
		}
		
		return null;
	}

	@Override
	public boolean drop(View view, int x, int y) {
		// TODO Auto-generated method stub
		// Set view.setVisibility(VISIBLE); if we return true
		return false;
	}

	@Override
	public void onDragIn(int x, int y) {
		final View view = findView(x, y);
		if (view != null && view != mLastTouched) view.performClick();
	}

	@Override
	public void onDragOut() {
		// TODO Auto-generated method stub
		
	}
	
}

